<html>
<head><meta charset="utf-8"><title>how does this type-check? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20does.20this.20type-check.3F.html">how does this type-check?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238668988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20does%20this%20type-check%3F/near/238668988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20does.20this.20type-check.3F.html#238668988">(May 13 2021 at 19:34)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">c</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">add</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">a</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">i32</span><span class="o">|</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">sub</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">|</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">b</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">42</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">add</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">sub</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>the closures have to be being coerced to function pointers, right? but why / where does rustc decide to do that? as far as I can see, <code>try_find_coercion_lub</code> only has a special case for <code>FnDef</code> which shouldn't get hit, and then I'd expect it to just try to coerce the one closure type to the other and vice versa and fail.</p>



<a name="238669526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20does%20this%20type-check%3F/near/238669526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20does.20this.20type-check.3F.html#238669526">(May 13 2021 at 19:37)</a>:</h4>
<p>I would expect that this happens somewhere around here: <a href="https://github.com/rust-lang/rust/blob/952c5732c2d25a875f90e5cd5dd29a1a21c1d4a2/compiler/rustc_typeck/src/check/coercion.rs#L993">https://github.com/rust-lang/rust/blob/952c5732c2d25a875f90e5cd5dd29a1a21c1d4a2/compiler/rustc_typeck/src/check/coercion.rs#L993</a></p>



<a name="238669759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20does%20this%20type-check%3F/near/238669759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20does.20this.20type-check.3F.html#238669759">(May 13 2021 at 19:39)</a>:</h4>
<p>ah wait, is my rustc source too old <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span></p>



<a name="238669868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20does%20this%20type-check%3F/near/238669868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20does.20this.20type-check.3F.html#238669868">(May 13 2021 at 19:40)</a>:</h4>
<p>I didn't expect this to be a new(ish) thing</p>



<a name="238670547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20does%20this%20type-check%3F/near/238670547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20does.20this.20type-check.3F.html#238670547">(May 13 2021 at 19:45)</a>:</h4>
<p>ok thanks, on the bright side, it actually was exactly where I expected it to be</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>